<template>
	<view class="contianer">
		<view class="project-search-box">
			<view class="select-project">
				<uni-data-select v-model="current.projectName" :localdata="prolist" placeholder="选择项目"
					@change="onchange"></uni-data-select>
			</view>
			<view class="search">
				<uni-easyinput prefixIcon="search" v-model="current.ydNumber" placeholder="请输入样地编号"
					@iconClick="iconClick">
				</uni-easyinput>
			</view>
		</view>
		<view class="project-list">
			<uni-collapse ref="collapse" accordion @change="change">
				<uni-collapse-item class="project-list-item" titleBorder="none" v-for="(item,index) in poltlist">
					<template v-slot:title>
						<uni-list>
							<uni-list-item>
								<template v-slot:body>
									<view class="slot-box cap">
										<uni-tag :circle="true" text="人工" type="success"
											v-if="item.grasslandType=='人工草地'" />
										<uni-tag :circle="true" text="天然" type="primary" v-else />

									</view>
									<view class="slot-box  title-ydmumber">
										<text class="slot-text">{{item.ydNumber}}</text>
									</view>
									<view class="status leftcap" style="color:green" v-if='item.status==0'>
										未调查
									</view>
									<view class="status leftcap" style="color:red" v-else>
										已调查
									</view>
								</template>
							</uni-list-item>
						</uni-list>
					</template>
					<view class="content">

						<view class="squarelist" v-for="(square,key) in squares"
							@click="gotoSquire(square,item.grasslandType)">
							<!-- <view class="square-icon"></view> -->
							<view class="square">{{square.yfNumber}}</view>
						</view>
						<view class="button-wrap">
							<view class="btn-item" v-for="(btn,key ) in btns" @click="toCreate(item,btn)">{{btn}}调查
							</view>
						</view>

					</view>
				</uni-collapse-item>
			</uni-collapse>
		</view>
		<view class="bottom-btn">
			<view class="btn" @click="addPlot">新增样地</view>
		</view>
	</view>
</template>

<script>
	import {
		projects,
		getYdList
	} from '@/api/system.js'
	import {
		getInfo
	} from '@/core/unicommon/storyageInfo.js'
	export default {
		data() {
			return {
				searchValue: null,
				current: {
					projectName: undefined,
					surveyUser: undefined,
					ydNumber: undefined
				},
				userdata: null,
				currentProject: null,
				network: true,
				allProject: [],
				prolist: [],
				poltlist: [],
				btns: [],
				squares: []
			}
		},
		onPullDownRefresh() {
			this.userdata = uni.getStorageSync("userdata")
		
			this.getProjects()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onLoad() {
			this.network = uni.getStorageSync("network")

			this.userinfo = uni.getStorageSync("userinfo")
			this.current.surveyUser = this.userinfo.id
			this.userdata = uni.getStorageSync("userdata")
			this.getProjects()
		},
		methods: {

			/**
			 * 跳转至样方
			 * @param {Object} square
			 */
			gotoSquire(square, grasslandType) {
				console.log(square)
				square.grasslandType = grasslandType
				if (square.surveyType == "盛期") {
					uni.navigateTo({
						url: "/pages/sampleplot/samplesquare/peakPeriodSquare?data=" + encodeURIComponent(JSON
							.stringify(square))

					})
				} else if (square.surveyType == "返青期") {
					uni.navigateTo({
						url: "/pages/sampleplot/samplesquare/greenSquare?data=" + encodeURIComponent(JSON
							.stringify(square))

					})
				} else if (square.surveyType == "枯黄") {
					uni.navigateTo({
						url: "/pages/sampleplot/samplesquare/yellowSquare?data=" + encodeURIComponent(JSON
							.stringify(square))

					})
				} else {
					uni.showToast({
						title: '样方缺少调查类型。默认为盛期',
						icon: 'none'
					})
					uni.navigateTo({
						url: "/pages/sampleplot/samplesquare/peakPeriodSquare?data=" + encodeURIComponent(JSON
							.stringify(square))

					})
				}


			},
			search(val) {
				console.log(val)
			},
			toCreate(item, type) {
				console.log(type, item)
				if (type == '返青期') {
					uni.navigateTo({
						url: "/pages/sampleplot/sampleplot/greenPlot?data=" + encodeURIComponent(JSON.stringify(
							item))

					})

				} else if (type == '盛期') {

					uni.navigateTo({
						url: "/pages/sampleplot/sampleplot/peakPeriodPlot?data=" + encodeURIComponent(JSON
							.stringify(item))

					})


				} else {
					uni.navigateTo({
						url: "/pages/sampleplot/sampleplot/yellowPlot?data=" + encodeURIComponent(JSON.stringify(
							item))

					})
				}
				// if(item.grasslandType)

			},

			getProjects() {
				let that = this
				this.allProject = this.userdata.projects
				this.prolist = this.allProject?.map(d => {
					return {
						text: d.projectName,
						value: d.projectName
					}
				})
				this.current.projectName = this.prolist[0].value
				this.getydData()
				this.getCurrentProject()
			},
			getydData() {
				if (!this.current.ydNumber) {
					this.poltlist = this.userdata.yd.filter(t => t.projectName == this.current.projectName && t
						.surveyUser == this.current.surveyUser)
				} else {
					this.poltlist = this.userdata.yd.filter(t => t.projectName == this.current.projectName && t
						.surveyUser == this.current.surveyUser && t.ydNumber == this.current.ydNumber)
				}
				console.log("123", this.userdata, this.poltlist)

			},
			getCurrentProject() {
				let cProjct = this.allProject.find(t => t.projectName == this.current.projectName)
				this.btns = cProjct.surveyType.split(',')
			},
			change(e) {
				if (!e || e == '') {
					return
				}
				let yd = this.poltlist[e]
				this.squares = this.userdata.yf.filter(t => t.ydNumber === yd.ydNumber && (t?.action!='delete'))

			},
			onchange(project) {
				this.getydData()
				this.getCurrentProject()
			},
			addPlot() {
				let cProjct = this.allProject.find(t => t.projectName == this.current.projectName)
				console.log(cProjct, this.current)
				uni.navigateTo({
					url: "/pages/sampleplot/sampleplot/addPolt?data=" + encodeURIComponent(JSON.stringify(cProjct))
				});
			},
			iconClick() {

			}
		}

	}
</script>

<style lang="scss" scoped>
	.contianer {
		height: 100vh;
		background-color: #eee;

		.project-search-box {
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			box-sizing: border-box;
			padding: 20rpx 15rpx;

			.select-project {
				width: 35%;
				// text-overflow: ellipsis;
				// overflow: hidden;
				// word-break: break-all;
				// white-space: nowrap;
			}

			.search {
				width: 65%;
				border-radius: 40rpx;
				overflow: hidden;
			}
		}

		.project-list ::v-deep {
			width: 100%;
			box-sizing: border-box;
			padding: 10rpx 20rpx 120rpx 20rpx;
		

			.uni-collapse {
				background-color: unset;
			}

			.project-list-item {
				margin-bottom: 20rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
				overflow: hidden;
				background-color: #fff;

				.list-item-title {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					box-sizing: border-box;
					padding: 15rpx 20rpx;
					position: relative;

					.list-type {
						display: inline-block;
						box-sizing: border-box;
						padding: 0 20rpx;
						height: 1.6em;
						line-height: 1.6em;
						background-color: #ffa502;
						color: #fff;
						border-radius: .8em;
						margin-right: 10rpx;
					}

					.list-status {
						float: right;
						position: absolute;
						right: 10rpx;
					}
				}

				.content {
					box-sizing: border-box;
					padding-bottom: 30rpx;
				}

				.uni-section-header {
					display: none;
				}

				.button-wrap {

					width: 100%;
					display: flex;
					justify-content: flex-start;
					flex-wrap: wrap;
					align-items: flex-start;
					box-sizing: border-box;
					padding: 10rpx 20rpx;

					.btn-item {
						box-sizing: border-box;
						padding: 0 20rpx;
						font-size: 28rpx;
						height: 2em;
						line-height: 2em;
						background-color: #00aaff;
						border-radius: 1em;
						color: #fff;
						margin-right: 20rpx;
					}
				}
			}
		}

		.bottom-btn {
			width: 100%;
			background-color: #fff;
			z-index: 100;
			margin: 0 auto;
			position: fixed;
			bottom: 0;
			left: 0;
			display: flex;
			justify-content: center;
			align-items: center;
			box-sizing: border-box;
			padding: 20rpx 0;

			.btn {
				width: 60vw;
				height: 60rpx;
				border-radius: 30rpx;
				text-align: center;
				line-height: 60rpx;
				background-color: #00aaff;
				color: #fff;
				cursor: pointer;
			}
		}
	}

	.cap {
		margin-right: 25rpx;
		width: 100rpx;
	}
	.leftcap{
		font-size: 28rpx;
		width: 100rpx;
	}

	.title-ydmumber {
		width: calc(100% - 200rpx)
	}


	.squarelist {
		width: 100%;
		height: 60rpx;
		padding-left: 60rpx;

		.square-icon {
			width: 20rpx;
			height: 20rpx;
			margin: 15rpx;
			border-radius: 20rpx;
			border: 2px solid #1890ff;
			float: left;
		}

		.square {
			float: left;
			margin-left: 15px;
			height: 60rpx;
			line-height: 50rpx;
			font-size: 30rpx;
			color: #1890ff;
		}
	}

	.btns {
		width: 100%;
		height: 80rpx;

		.plotBtn {
			width: 25%;
			height: 60rpx;
			float: left;
			margin-left: 10rpx;
			background-color: #00aaff;
			line-height: 60rpx;
			border-radius: 30rpx;
			color: #fff;
			text-align: center;
			margin-top: 10rpx;
		}
	}
	::v-deep{
		 .uni-select__selector-item{
			 text-overflow: ellipsis;
			 overflow: hidden;
			 word-break: break-all;
			 white-space: nowrap;
		 }
	}
</style>